import java.io.*;
public class Demo29 {
    static int N = 1000010;
    static int n,k;
    static int hh,tt;
    static int[] q = new int[N];
    static int[] a = new int[N];
    public static void main(String[] args)throws IOException{
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter wt = new PrintWriter(new OutputStreamWriter(System.out));
        String[] st = bf.readLine().split(" ");
        n = Integer.parseInt(st[0]);
        k = Integer.parseInt(st[1]);
        String[] str = bf.readLine().split(" ");
        for(int i = 0 ; i < n ; i ++ ) a[i] = Integer.parseInt(str[i]);

        hh = 0;tt = - 1;
        for(int i = 0 ; i < n ; i ++ ){
            if(hh <= tt && q[hh] < i - k + 1) hh ++;
            while(hh <= tt && a[q[tt]] >= a[i]) tt --;  //求最小值所以保留最小值
            q[++ tt] = i;
            if(i >= k - 1) wt.print(a[q[hh]] + " ");
        }
        wt.println();
        hh = 0;tt = -1;
        for(int i = 0 ; i < n ; i ++){
            if(hh <= tt && q[hh] < i - k + 1) hh ++;
            while(hh <= tt && a[q[tt]] <= a[i]) tt --;//求最大值所以保留最大值

            q[++ tt] = i;
            if(i >= k - 1) wt.print(a[q[hh]] + " ");
        }
        wt.flush();//记得刷新流
    }
}
